/* Kishore */
/* Media Database */
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <stdint.h>

#include "mediadb.h"

main(int argc, char** args)
{

  uint8_t      **queries;
  sqlite3_stmt *stmt;
  sqlite3      *sqhandle;
  uint8_t      retval = SUCCESS;
  uint8_t      createtbl[CREATE_TABLE_CMD_LEN];
  uint8_t      inserttbl[INSERT_TABLE_CMD_LEN];


  queries = malloc(sizeof(uint8_t) * NUM_OF_QUERIES * SIZE_OF_QUERY);

  retval = sqlite3_open("mediadb.db", &sqhandle);

  if (retval) {
   printf("ERROR: sqlite3_open failure\n");
  } else {
   printf("sqlite3_open success\n");
  }


  /* Creating Table */
  sprintf(createtbl, "%s", CREATE_TABLE_CMD);
  printf("Createtbl 1 : %s\n", createtbl);

  sprintf(createtbl, "%s %s", createtbl, "users");
  printf("Createtbl 2 : %s\n", createtbl);

  sprintf(createtbl, "%s \(%s,", createtbl, MEDIADB_FIELD1);
  printf("Createtbl 4 : %s\n", createtbl);

  sprintf(createtbl, "%s %s,", createtbl, MEDIADB_FIELD2);
  printf("Createtbl 5 : %s\n", createtbl);

  sprintf(createtbl, "%s %s,", createtbl, MEDIADB_FIELD3);
  printf("Createtbl 6 : %s\n", createtbl);

  sprintf(createtbl, "%s %s,", createtbl, MEDIADB_FIELD4);
  printf("Createtbl 7 : %s\n", createtbl);

  sprintf(createtbl, "%s %s\)", createtbl, MEDIADB_FIELD5);
  printf("Createtbl 8 : %s\n", createtbl);


  retval = sqlite3_exec(sqhandle,createtbl,0,0,0);

  if (retval) {
   printf("ERROR: sqlite3_exec failure\n");
  } else {
   printf("sqlite3_exec success\n");
  }

  sprintf(inserttbl, "%s", INSERT_TABLE_CMD);
  printf("Inserttbl 1 : %s\n", inserttbl);

  sprintf(inserttbl, "%s %s", inserttbl, "users");
  printf("Inserttbl 2 : %s\n", inserttbl);

  sprintf(inserttbl, "%s %s", inserttbl, "VALUES(");
  printf("Inserttbl 3 : %s\n", inserttbl);

  sprintf(inserttbl, "%s%s", inserttbl, "\'x.mp3\',");
  printf("Inserttbl 4 : %s\n", inserttbl);

  sprintf(inserttbl, "%s%s", inserttbl, "10000,");
  printf("Inserttbl 5 : %s\n", inserttbl);

  sprintf(inserttbl, "%s%s", inserttbl, "\'Track 1\',");
  printf("Inserttbl 6 : %s\n", inserttbl);

  sprintf(inserttbl, "%s%s", inserttbl, "\'Artist 1\',");
  printf("Inserttbl 7 : %s\n", inserttbl);

  sprintf(inserttbl, "%s%s", inserttbl, "30\)");
  printf("Inserttbl 8 : %s\n", inserttbl);

  retval = sqlite3_exec(sqhandle,inserttbl,0,0,0);

  if (retval) {
   printf("ERROR: sqlite3_exec failure\n");
  } else {
   printf("sqlite3_exec success\n");
  }

}
